<div class="freq-show" style="padding:20px;display: none;">
    <form class="layui-form layui-form-pane" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">频道名称</label>
            <div class="layui-inline">
                <input type="text" id="name" class="layui-input" value="" autocomplete="off">
            </div>
        </div>
    </form>
</div>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index'],function () {
        var table = layui.table;
        var $ = layui.$
            ,admin = layui.admin;


        table.render({
            elem: '#LAY-novel-freq'
            , url: '/novel/freqList'
            , response: {
                statusCode: 1 //规定成功的状态码，本来是：0，现在统一改为1
            }
            , toolbar: '#freqToolbar' //开启头部工具栏，并为其绑定左侧模板
            , defaultToolbar: false
            , page: true //开启分页
            , cols: [[
                {field: 'id', width: 80, title: 'ID'}
                , {field: 'name', title: '频道名称', align: 'center'}
                , {field: 'class_count', title: '分类数量', align: 'center'}
                , {field: 'novel_count', title: '书籍数量', align: 'center'}
                , {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#table-useradmin-admin'}
            ]]
        });


        table.on('toolbar(LAY-novel-freq)', function (obj) {
            $("#name").val('');
            switch (obj.event) {
                case 'add':
                    layer.open({
                        type: 1,
                        title:'添加频道',
                        offset: '15px',
                        area: 'auth',
                        skin: 'layui-layer-demo', //样式类名
                        closeBtn: 0, //不显示关闭按钮
                        anim: 2,
                        shadeClose: true, //开启遮罩关闭
                        content: $('.freq-show'),
                        btn: ['确定', '取消']
                        ,yes: function(index, layero){
                            var name = $("#name").val();
                            admin.req({
                                url: '/novel/setFreq'
                                , type: "POST"
                                , data: {'name':name}
                                , done: function (res) {
                                    layer.msg('添加频道成功', {icon: 1, time: 1000}, function () {
                                        layer.close(index); //关闭弹层
                                        table.reload('LAY-novel-freq');
                                    });
                                },not_finish:function (res) {
                                    layer.msg(res.msg);
                                }
                            });
                        }
                    });
                    break;
            }
        });

        table.on('tool(LAY-novel-freq)', function(obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                $("#name").val(data.name);
                layer.open({
                    type: 1,
                    title:'编辑频道',
                    offset: '15px',
                    area: 'auth',
                    skin: 'layui-layer-demo', //样式类名
                    closeBtn: 0, //不显示关闭按钮
                    anim: 2,
                    shadeClose: true, //开启遮罩关闭
                    content: $('.freq-show'),
                    btn: ['确定', '取消']
                    ,yes: function(index, layero){
                        var name = $("#name").val();
                        admin.req({
                            url: '/novel/setFreq'
                            , type: "POST"
                            , data: {'name': name, 'id': data.id}
                            , done: function (res) {
                                layer.msg('编辑频道成功', {icon: 1, time: 1000}, function () {
                                    layer.close(index); //关闭弹层
                                    table.reload('LAY-novel-freq');
                                });
                            },not_finish:function (res) {
                                layer.msg(res.msg);
                            }
                        });
                    }
                });
            } else if (obj.event === 'del') {
                layer.confirm('确认删除此频道？', {icon: 3,title: false}, function (index) {
                    admin.req({
                        url: '/novel/delFreq'
                        , type: "POST"
                        , data: {'id': data.id}
                        , done: function (res) {
                            layer.msg('删除成功', {icon: 1, time: 1000}, function () {
                                obj.del();
                                layer.close(index);
                            });
                        }, not_finish: function (res) {
                            layer.msg(res.msg);
                        }
                    });
                });
            }
        });
    });
</script>